﻿$(function() {

var
	parse = function() {
		var server = $(document).data('SERVER');
		
		var year = (new Date(server.timestamp * 1000)).getFullYear();
		
		var ctx = {
			year: year, year1: year - 1, year2: year - 2
		};

		$('.template').each(function() {
			$(this).html(Mustache.render($(this).html(), ctx));
		});	
	},
	
	renderTrcHTML = function(idx) {
		var ctx = {idx: idx};
				
		$('input[type="radio"]:checked,input[type="text"],input[type="hidden"]', '#form-trc').each(function() {
			if (this.type == 'radio') {
				ctx[this.name.substr(0, this.name.length - 3)] = {
					text: $(this).attr("label"), val: this.value
				};
			} else {
				ctx[this.id] = {
					val: this.value
				};
			}
		});

		return Mustache.render($('#template-trc-tr').val(), ctx);
	},

	layout = function() {
		$('input[type="text"] ,input[type="file"]').addClass('text');
		$('#suc_industry,#suc_reg_industry').parent().addClass('large');
		
		$('label').each(function() {
			$(this).parent().addClass('input-item');
			$(this).siblings().wrapAll('<p class="wrap input"></p>');
			$(this).wrap('<p class="wrap label"></p>');
		});
	},
	
	prepare = function() {
		$("#tabs").tabs();

		$("#addtrc").button().click(function() {
			$("#dialog-trc").attr("idx", "").dialog("open");
			return false;
		});

		$("#submit").button().click(function() {
			return false;
		});
		
		$('#form-main').append('<input type="hidden" name="trclistCounter" id="trclistCounter" value="" />');
	},
	
	setTrcListCounter = function() {
		var counter = "";
		$('#list-trc tbody>tr').each(function() {
			counter += $(this).data('idx') + ',';
		});
		
		$('#trclistCounter').val(counter);
	},
	
	editTrc = function(jqTr) {
		var idx = jqTr.data('idx');

		$('#dialog-trc input[type="text"]').each(function() {
			$(this).val($('#' + this.id + '_' + idx).val());
		});

		$('#dialog-trc input[type="radio"]').each(function() {
			if ($(this).val() == $('#' + this.name + '_' + idx).val()) {
				$(this).attr('checked', 'checked');
			}			
		});

		$('#dialog-trc').data('selected', jqTr);
		$('#dialog-trc').dialog('open');
	},
	
	removeTrc = function(jqTr) {
		var idx = jqTr.data('idx');

		$('#dialog-trc-confirm').dialog({
			resizable: false,
			height: 200,
			modal: true,
			buttons: {
				"确定": function() {
					jqTr.remove();
					$(this).dialog("close");
					setTrcListCounter();
				},
				"取消": function() {
					$(this).dialog("close");
				}
			}
		});
	}
	
	attachTrcEvent = function(jqTr) {
		$('.edit', jqTr).click(function() {
			editTrc(jqTr);
		});
		$('.remove', jqTr).click(function() {
			removeTrc(jqTr);
		});
	},
	
	onTrcNew = function(jqDlg) {
		var counter = jqDlg.data('counter') || 0;
		var html = '<tr>' + renderTrcHTML(counter) + '</tr>';
		var jqTr = $(html).appendTo('#list-trc tbody').data('idx', counter);
		
		jqDlg.data('counter', counter + 1);
		setTrcListCounter();

		return jqTr;
	},

	onTrcEdit = function(jqDlg) {
		var jqTr = jqDlg.data('selected');
		jqTr.html(renderTrcHTML(jqTr.data('idx')));
		jqDlg.removeData('selected');
		
		return jqTr;
	},
	
	view = function() {
		new $.grooveView('add-suc-apply', {
			submitMessage: "供应商入驻申请资料提交",
			OKMessage: "已成功提交入驻申请资料",
			submitButtonID: "submit",
			formID: "form-main",
			"dialog-trc": function(jqDlg) {
				var jqTr;
				
				if (!jqDlg.data('selected')) {
					jqTr = onTrcNew(jqDlg);
				} else {
					jqTr = onTrcEdit(jqDlg);
				}
				
				jqDlg.find('form').resetForm();
				attachTrcEvent(jqTr);
			}
		});	
	};
	
	parse();
	layout();
	prepare();
	view();
});